package com.lanou.controller;

import com.lanou.bean.AdminInfo;
import com.lanou.bean.AdminModule;
import com.lanou.bean.AdminRole;
import com.lanou.service.AdminInfoService;
import com.lanou.service.AdminRoleService;
import com.lanou.utils.AjaxResult;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by dllo on 18/3/12.
 */
@Controller
public class AdminController {
    @Resource
    private AdminInfoService adminInfoService;
    @Resource
    AdminRoleService adminRoleService;

    @RequestMapping(value = "/admin_list")
    public String admin_list() {
        if (SecurityUtils.getSubject().isPermitted("管理员")) {
            return "admin/admin_list";
        } else {
            return "nopower";
        }
    }

    @RequestMapping(value = "/admin_add")
    public String admin_add() {
        return "admin/admin_add";
    }

    @RequestMapping(value = "/admin_modi")
    public String admin_modi() {
        return "admin/admin_modi";
    }

    /**
     * 查询管理员信息和对应角色
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectAdminModule")
    public List<AdminModule> selectAdminModule() {

        return adminInfoService.selectAdminModule();
    }

    /**
     * 根据管理员id查询管理员信息和角色信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectAdminModuleById")
    public AdminModule selectAdminModuleById(@RequestParam("adminId") int id) {
        return adminInfoService.selectAdminModuleById(id);
    }

    /**
     * 根据管理员ID修改管理员信息
     * @param id
     * @param name
     * @param namecode
     * @param tel
     * @param email
     * @param roles
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updataAdmin")
    public AjaxResult updataAdmin(@RequestParam("adminId") int id, @RequestParam("name") String name,
                                  @RequestParam("namecode") String namecode, @RequestParam("tel") String tel,
                                  @RequestParam("email") String email, @RequestParam("roles[]") List<Integer> roles) {

        adminInfoService.upDataAdminInfo(name, namecode, tel, email, id);
        adminRoleService.deleteRoleByAdminId(id);
        for (int i = 0; i < roles.size(); i++) {
            adminRoleService.insertRoleByAdminId(id, roles.get(i));
        }

        AjaxResult result = new AjaxResult();
        result.setMsg("admin_list");
        return result;
    }

    /**
     * 个人信息修改模块
     * 根据管理员名修改管理员信息
     * @param name
     * @param tele
     * @param email
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateAdminInfo")
    public AjaxResult updateAdminInfo(@RequestParam("name") String name,
                                      @RequestParam("tele") String tele,
                                      @RequestParam("email") String email) {
        Subject subject = SecurityUtils.getSubject();
        AdminInfo adminInfo = (AdminInfo) subject.getPrincipal();
        adminInfoService.updateAdmin(name, tele, email, adminInfo.getAdminId());
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setMsg("/userInfo");
        return ajaxResult;
    }

    /**
     * 修改密码模块
     * 修改密码
     * @param oldPsw
     * @param newPsw
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updatePsw")
    public AjaxResult updatePsw(@RequestParam("oldPsw") String oldPsw,
                                @RequestParam("newPsw") String newPsw) {
        Subject subject = SecurityUtils.getSubject();
        AdminInfo adminInfo = (AdminInfo) subject.getPrincipal();
        String password = adminInfo.getPassword();
        AjaxResult ajaxResult = new AjaxResult();
        if (oldPsw.equals(password)){
            adminInfoService.updataPsw(newPsw,adminInfo.getAdminId());
            ajaxResult.setMsg("/logout");
            return ajaxResult;
        }else {
            ajaxResult.setMsg("/error");
            return ajaxResult;
        }
    }



}
